<?php
/**
 * @Name: NodeSearch.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Search\Rbac;

use App\Http\Search\BaseSearch;
use App\Http\Model\Rbac\RoleModel;
use HugCode\PhpUnits\Frame\Code\ConstCode;

class RoleSearch extends BaseSearch
{

    /**
     * @Desc 列表
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function index()
    {
        $where  = [
            'RoleStatus' => _array_field($this->params, 'Status', ConstCode::BASIC_STATUS_NORMAL)
        ];
        $query  = RoleModel::query()->with('node')->with('node.node')->where($where);
        $result = $this->pageList($query);

        $childrenMap = [];
        if (!empty($result['list'])) {
            foreach ($result['list'] as $item) {
                foreach ($item['node'] as $info) {
                    if (is_array($info)) {
                        $childrenMap[$info['node']['ParentID']] = $info['node']['NodeID'];
                    }
                }
            }
            foreach ($result['list'] as &$val) {
                $nodeList = [];
                foreach ($val['node'] as $node) {
                    if (!isset($childrenMap[$node['NodeID']])) {
                        $nodeList[] = $node['NodeID'];
                    }
                }
                $val['NodeID'] = $nodeList;
                unset($val['node']);
            }
        }
        return $result;
    }

    /**
     * @Desc 输入框远程搜索
     * @return array
     * @author yashuai<1762910894@qq.com>
     */
    public function select()
    {
        $query  = RoleModel::query()->where(['RoleStatus' => ConstCode::BASIC_STATUS_NORMAL]);
        $select = $this->selectBase($query, 'RoleName', 'RoleID');
        return ['select' => $select];
    }

}
